﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BL_Recorder
{
    public class Game
    {
        //两次投掷未补中或全中
        public int GetScore()
        {
            return scoreForeFrame(getCurrentFrame()-1);
        }

        public void Add(int pias)
        {
            itsThrows[itsCurrentThrew++] = pias;
            itsScore += pias;
            adjustCurrentFrame(pias);
        }
        private void adjustCurrentFrame(int pias)
        {
            if (firstThrowInFrame == true)
            {
                if (pias == 10)
                    itsCurrentFrame++;
                else
                    firstThrowInFrame = false;
            }
            else
            {
                firstThrowInFrame = true;
                itsCurrentFrame++;
            }
            itsCurrentFrame = Math.Min(11, itsCurrentFrame);
        }
        public int getCurrentFrame()
        {
            return itsCurrentFrame;
        }

        //补中的情况
        public int scoreForeFrame(int theframe)
        {
            int socre = 0;
            ball = 0;
            for (int crrentFrem = 0; crrentFrem < theframe; crrentFrem++)
            {

                if (Strike())
                {
                    socre += 10 + NextTowBallsForStrike();
                    ball++;
                }
                else if (Spare())
                {

                    socre += 10 + NextBallForSpare();
                    ball += 2;

                }
                else
                {
                    socre += TowBallInFrame();
                    ball += 2;

                }
            }
            return socre;
        }
        //判断全中
        private bool Strike()
        {
            return itsThrows[ball] == 10;
        }
        //判断补中
        private bool Spare()
        {
            return (itsThrows[ball] + itsThrows[ball + 1]) == 10;
        }
        //全中奖励分
        private int NextTowBallsForStrike()
        {
            return itsThrows[ball + 1] + itsThrows[ball + 2];
        }
        //补中奖励分
        private int NextBallForSpare()
        {
            return itsThrows[ball + 2];
        }
        //没有全中和补中分数
        private int TowBallInFrame()
        {
            return itsThrows[ball] + itsThrows[ball + 1];
        }
        private int ball;
        private int secondThrow;
        private int itsScore = 0;
        private int[] itsThrows = new int[21];
        private int itsCurrentThrew = 0;
        private int itsCurrentFrame = 1;
        private int firsThrow;
        private Boolean firstThrowInFrame = true;
    }
}
